import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:laos_mall/common/base/base_controller.dart';
import 'package:laos_mall/common/base/page_size_controller.dart';
import 'package:laos_mall/common/utils/http_util.dart';
import 'package:laos_mall/common/widght/dropdown/custom_dropdown.dart';
import 'package:laos_mall/models/category/category_model.dart';
import 'package:laos_mall/models/category/para_product.dart';

class CatergoryLogic extends BaseController with GetTickerProviderStateMixin{
  late TabController tabController;
   TabController? tabController1;
  late TabController tabController2;
  List<Map> categories = [];
  GlobalKey btnKey = GlobalKey();
  final  dropDownKey = GlobalKey<PositionedDropdownState>();
  CategoryModel? model;

  int selIndex0 = 0;
  int selIndex1 = 0;
  int selIndex2 = 0;
  @override
  void onInit() {
    tabController = TabController(length: 2, vsync: this);
    tabController1 = TabController(length: 3, vsync: this);
    tabController2 = TabController(length: 4, vsync: this);
    categories = List.generate(4, (index) => {"name":"item$index","list":["1","2","3"]}).toList();

    super.onInit();
  }

  @override
  Future<void> Function()? get request => ()async{
    var req = await HttpUtil.restApi.getAllCategory({});
    model = req.data;
    makeTabController();

};

  makeTabController(){
    tabController = TabController(length: model?.category_tree?.length ?? 2, vsync: this);
    tabController1 = TabController(length: model?.category_tree?[0].children?.length ?? 0, vsync: this);
    tabController2 = TabController(length: model?.category_tree?[0].children?[0].children?.length ?? 0, vsync: this);

  }

  jumpTabIndex(Category_tree category1,{Category_tree? category2,Category_tree? category3}){
    tabController.animateTo(model?.category_tree?.indexOf(category1) ?? 0);
    if(category2 != null) {
      selIndex1 = category1.children?.indexOf(category2) ?? 0;
      // tabController1?.animateTo(category1.children?.indexOf(category2) ?? 0);
      if(category3!= null){
        tabController2.animateTo(category2.children?.indexOf(category3) ?? 0);
      }
    }

    update();
  }

  @override
  void dispose() {
    tabController.dispose();
    tabController1?.dispose();
    tabController2.dispose();
    super.dispose();
  }
}
